﻿using DHQGHN.DAL.Base;
using DHQGHN.DAL.Common;
using DHQGHN.DAL.Models.DHQG;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DHQGHN.DAL.DAO.DHQG
{
    public class DonVi_ChiTieuDAO : SqlDAO
    {
        public DonVi_ChiTieuDAO(string connectionString, System.Type type = null) :
            base(connectionString, type) { ;}
        public const string PROCEDURE_INSERT = "spDonViChiTieu_Insert";
        public const string PROCEDURE_GET_ALL_BY_NKH = "spDonViChiTieu_GetAllByNKH";
        public const string PROCEDURE_DELETE_BY_NKH = "spDonViChiTieu_DeleteByNKH";
        public const string PROCEDURE_DELETE_RIENG_BY_NKH = "spDonViChiTieu_Rieng_DeleteByNKH";
        public const string PROCEDURE_GET_BY_DONVI = "spDonViChiTieu_GetByDonvi";
        
        public const string PARAM_TABLE = "@Table";
        public const string PARAM_NAM_KE_HOACH_ID = "@NAM_KE_HOACH_ID";
        public const string PROCEDURE_GET_CHI_TIEU_RIENG_BY_DV = "spChiTieu_Chi_Tieu_Rieng_BY_DV";
        public const string PARAM_FLAG = "@FLAG";
        public const string PARAM_DON_VI_ID = "@DON_VI_ID";
        public bool Save(List<DonViChiTieu> data)
        {
            try
            {
                DataTable dataTable = GetDataTable(data);
                SqlCommand cmd = this.GetSprocCommand(PROCEDURE_INSERT);
                cmd.Parameters.Add(new SqlParameter(PARAM_TABLE, dataTable));
                int result = ExecuteNonQuery(cmd);

                return result > 0 ? true : false;
            }
            catch
            {
                throw;
            }
        }
        public int DeleteDonViChiTieuByNKH(int namkhid, int donviid,int flag)
        {
            try
            {

                SqlCommand cmd = this.GetSprocCommand(PROCEDURE_DELETE_BY_NKH);
                cmd.Parameters.Add(new SqlParameter(PARAM_NAM_KE_HOACH_ID, namkhid));
                cmd.Parameters.Add(new SqlParameter(PARAM_DON_VI_ID, donviid));
                cmd.Parameters.Add(new SqlParameter(PARAM_FLAG, flag));
                return ExecuteNonQuery(cmd);
            }
            catch
            {
                throw;
            }
        }
        public int DeleteDonViChiTieuRiengByNKH(int namkhid, int donviid)
        {
            try
            {

                SqlCommand cmd = this.GetSprocCommand(PROCEDURE_DELETE_RIENG_BY_NKH);
                cmd.Parameters.Add(new SqlParameter(PARAM_NAM_KE_HOACH_ID, namkhid));
                cmd.Parameters.Add(new SqlParameter(PARAM_DON_VI_ID, donviid));
                return ExecuteNonQuery(cmd);
            }
            catch
            {
                throw;
            }
        }
        public List<DonViChiTieu> GetAllDvCtbyNKH(int namkhid, int flag)
        {
            try
            {

                SqlCommand cmd = this.GetSprocCommand(PROCEDURE_GET_ALL_BY_NKH);
                cmd.Parameters.Add(new SqlParameter(PARAM_NAM_KE_HOACH_ID, namkhid));
                cmd.Parameters.Add(new SqlParameter(PARAM_FLAG, flag));
                return GetList<DonViChiTieu>(cmd);
            }
            catch
            {
                throw;
            }
        }
        public List<DonViChiTieu> GetAllByDV(int namkhid, int donviid)
        {
            try
            {

                SqlCommand cmd = this.GetSprocCommand(PROCEDURE_GET_BY_DONVI);
                cmd.Parameters.Add(new SqlParameter(PARAM_NAM_KE_HOACH_ID, namkhid));
                cmd.Parameters.Add(new SqlParameter(PARAM_DON_VI_ID, donviid));
                return GetList<DonViChiTieu>(cmd);
            }
            catch
            {
                throw;
            }
        }
        public List<DonViChiTieu> GetChitieuriengbydv(int namkhid, int donviid)
        {
            try
            {

                SqlCommand cmd = this.GetSprocCommand(PROCEDURE_GET_CHI_TIEU_RIENG_BY_DV);
                cmd.Parameters.Add(new SqlParameter(PARAM_NAM_KE_HOACH_ID, namkhid));
                cmd.Parameters.Add(new SqlParameter(PARAM_DON_VI_ID, donviid));
                return GetList<DonViChiTieu>(cmd);
            }
            catch
            {
                throw;
            }
        }
        private DataTable GetDataTable(List<DonViChiTieu> data)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("DON_VI_CHI_TIEU_ID");
            dt.Columns.Add("DON_VI_ID");
            dt.Columns.Add("CHI_TIEU_ID");
            dt.Columns.Add("NAM_KE_HOACH_ID");
            dt.Columns.Add("FLAG");
            foreach (var item in data)
            {
                dt.Rows.Add(item.DON_VI_CHI_TIEU_ID, item.DON_VI_ID, item.CHI_TIEU_ID,item.NAM_KE_HOACH_ID,item.FLAG);
            }
            return dt;
        }
    }
}
